package com.sundhage.net
{
	import com.sundhage.util.JSXMLHelper;
	
	import flash.events.*;
	import flash.net.*;
	import flash.xml.*;
	
	public class JSPHPComm extends EventDispatcher {
		
		public static var COMPLETE:String = "jsphpcommcomplete";
		public static var RETURNTYPE_XML:String = "retxml";
		public static var RETURNTYPE_VARS:String = "retvars";
		public var data:Object;
		public var xml:XMLDocument;
		
		private var verbose:Boolean;
		private var url:String;
		private var type:String;
		
		private function ioError(e:Event):void {
			trace("JSPHPComm: io error");
		}
		private function secError(e:Event):void {
			trace("JSPHPComm: security error");
		}
		
		// xml tillbaka 
		
		/*
				se JSXMLHelper för mer info
		*/
		private function parseXml(sXml:String):void {
			xml = new XMLDocument(sXml);
			data = JSXMLHelper.XML2OBJ(xml, true);
			dispatchEvent(new Event(COMPLETE));
		}
		
		// vars tillbaka, om enumerering: skapa array
		
		/*
			enumrerade variabler som börjar på 0 kommer automagiskt bli en array med
			variabelnamnet. ex: id0=1&id1=2&id3=44 --> id[0, 1, 2, 3]
			andra variabler blir strings.
			
			SAMTLIGA variabler unescapeas
		*/
		
		private function parseVars(sVars:String):void {
			var vars:URLVariables = new URLVariables(sVars);
			data = new Object();
			for (var item:String in vars) {
				// om item slutar på 0 --> gör array
				
				if (item.substring(item.length-1, item.length) == "0" &&
					(parseInt(item.substring(item.length-2, item.length-1)) != parseInt(item.substring(item.length-2, item.length-1)))) {
					
					var itemName:String = item.substring(0, item.length-1);
					var c:int = 0;
					data[itemName] = new Array();
					while (vars[itemName+c]) {
						data[itemName].push(unescape(vars[itemName+c]));
						c++;
					}
					
				} else if (parseInt(item.substring(item.length-1, item.length)) > -1) {
					// skit i dessa, ordnas ju i while-loopen
				} else {
					data[item] = unescape(vars[item]);
				}
			}
			dispatchEvent(new Event(COMPLETE));			
		}
		
		private function dataLoaded(e:Event):void {
			if (verbose) trace("JSPHPComm Data from server: "+e.target.data);
			if (type == RETURNTYPE_VARS) parseVars(e.target.data);
			else if (type == RETURNTYPE_XML) parseXml(e.target.data);
			else trace("JSPHPComm: no returntype defined!");
		}
		
		public function send(o:Object):void {			
			var vars:URLVariables = new URLVariables();
			for (var item:String in o) {
				vars[item] = escape(o[item]);
				//trace(vars[item]);
			}
			var req:URLRequest = new URLRequest(url);
			req.method = URLRequestMethod.POST;
			req.data = vars;
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, dataLoaded);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, secError);
			loader.addEventListener(IOErrorEvent.IO_ERROR, ioError);
			loader.load(req);
		}
		
		
		public function JSPHPComm(url:String, type:String = "", verbose:Boolean = false) {
			this.verbose = verbose;
			if (type == "") type = RETURNTYPE_XML;
			this.url = url;
			this.type = type;
		}

	}
}